#define _GNU_SOURCE // 如是不声明gettid()不识别报错
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *first_func(void *arg)
{
    char *str = (char *)arg;
    printf("first线程(%d)开始执行, arg=%s \n", gettid(), str);
    return "abc";
}

void *second_func(void *arg)
{
    char c = *(char *)arg;
    printf("second线程(%d)开始执行, arg=%c \n", gettid(), c);

    return (void *)2;
}

int main(int argc, char const *argv[])
{
    printf("main线程(%d)开始执行 \n", gettid());

    // 创建分线程
    pthread_t first_thread;
    pthread_t second_thread;
    pthread_create(&first_thread, NULL, first_func, "abcd");
    char c = 'a';
    pthread_create(&second_thread, NULL, second_func, &c);
    printf("main线程(%d)继续执行 \n", gettid());
    
    // 等待分线程执行完毕
    char *fisrt_result;
    int second_result;
    pthread_join(first_thread, (void **)&fisrt_result);
    pthread_join(second_thread, (void **)&second_result);
   
    printf("main线程(%d)继续执行， 即将结束 \n", gettid());
    printf("first线程返回值：%s \n", fisrt_result);
    printf("second线程返回值：%d \n", second_result);
    return 0;
}
